<template>
  <EditOrViewTable :table-data="tableData" :type="type" :show-btn="false" :setting="setting" />
</template>
<script>
import EditOrViewTable from './editOrViewTable'
export default {
  name: 'ShopConstructionInfo',
  components: {
    EditOrViewTable
  },
  props: {
    tableData: {
      type: Array,
      default: () => []
    },
    type: {
      type: String,
      default: 'view'
    }
  },
  data() {
    return {
      setting: [
        {
          prop: 'shopCode',
          label: '门店编码',
          minWidth: 100
        },
        {
          prop: 'shopName',
          label: '门店名称',
          minWidth: 140
        },
        {
          prop: 'constCode',
          label: '门店建设项目号',
          minWidth: 140
        },
        {
          prop: 'belongStage',
          label: '所属阶段',
          minWidth: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'BELONG_STAGE'
          }
        },
        {
          prop: 'handleType',
          label: '门店建设数据处理方式',
          minWidth: 140,
          editRender: (h, row) => {
            return h('c-dict-select', {
              props: {
                dictTypeCode: 'CTM_SHOP_CONS_DATA_PROCESS_TYPE',
                value: row.handleType
              },
              attrs: {
              },
              on: {
                change: v => {
                  this.$set(row, 'handleType', v)
                  if (!v) return
                  let message = '请及时到SLM系统-门店建设跟进管理-门店建设整体跟进板块-勾选要交接的门店，选择“市场交接”按钮提交申请！'
                  if (v === 'A') {
                    message = '请及时到SLM系统-门店建设跟进管理-门店建设整体跟进模块，勾选要终止的门店，选择“建设终止”按钮提交申请'
                  }
                  setTimeout(() => {
                    this.findChild(this.$children)
                    this.$msgbox({
                      title: '',
                      message,
                      closeOnClickModal: false,
                      center: true
                    })
                  }, 0)
                }
              }
            })
          },
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'CTM_SHOP_CONS_DATA_PROCESS_TYPE'
          }
        }
      ]
    }
  },
  methods: {
    validate() {
      return new Promise((resolve, reject) => {
        const errMsgs = []
        for (let i = 0; i < this.tableData.length; i++) {
          const item = this.tableData[i]
          if (!item.handleType) {
            errMsgs.push(`【经销商-门店建设信息】第${i + 1}行, 门店建设数据处理方式未选择!`)
          }
        }
        if (errMsgs.length > 0) {
          this.$message({
            type: 'warning',
            message: errMsgs.join('<br>'),
            dangerouslyUseHTMLString: true
          })
          reject(false)
          return
        }
        resolve(true)
      })
    },
    findChild(children, name = 'ElSelect') {
      for (const c of children) {
        if (c.$options.name === name && c.blur) {
          c.blur()
        }
        if (c.$children) {
          this.findChild(c.$children, name)
        }
      }
    }
  }
}
</script>
